package br.com.educa.modelo.financeiro;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.serializable.RooSerializable;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

import br.com.educa.modelo.publico.Controle;
import br.com.educa.modelo.security.Usuario;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

@Entity
@Table
@Configurable
@RooSerializable
@RooJson
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Negociacoes implements Serializable {

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;
	
	//unidade que controla essa negociação.
	@ManyToOne
	@JoinColumn(name = "controle", referencedColumnName = "id")
	private Controle controle;
	
	//lançamentos dos alunos no sistema.
    @OneToMany(mappedBy = "negociacao")
    private List<LancamentosAlunos> negociacaoLancamentosAlunos;
    
    //movimentações nos caixas.
    @OneToMany(mappedBy = "negociacao")
    private List<MovimentacoesCaixas> negociacaoMovimentacaoCaixas;
    
    //data da negociação.
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(style = "M-")
    private Date dataNegociacao;
    
    @ManyToOne
    @JoinColumn
    private Usuario autorizador;
    
    private Double totalNegociacao;
    
    //desconto dado na negociação no total.
    private Double descontoReais = 0.00d;
    
    private Double totalNegociacaoPagar;
    
    private String justificativaNegociacao;

	public Double getTotalNegociacaoPagar() {
		return totalNegociacaoPagar;
	}

	public void setTotalNegociacaoPagar(Double totalNegociacaoPagar) {
		this.totalNegociacaoPagar = totalNegociacaoPagar;
	}

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Negociacoes().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countNegociacoeses() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Negociacoes o", Long.class).getSingleResult();
    }

	public static List<Negociacoes> findAllNegociacoeses() {
        return entityManager().createQuery("SELECT o FROM Negociacoes o", Negociacoes.class).getResultList();
    }

	public static Negociacoes findNegociacoes(Long id) {
        if (id == null) return null;
        return entityManager().find(Negociacoes.class, id);
    }

	public static List<Negociacoes> findNegociacoesEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Negociacoes o", Negociacoes.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Negociacoes attached = Negociacoes.findNegociacoes(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Negociacoes merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Negociacoes merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	private static final long serialVersionUID = 1L;

	public Date getDataNegociacao() {
        return this.dataNegociacao;
    }

	public void setDataNegociacao(Date dataNegociacao) {
        this.dataNegociacao = dataNegociacao;
    }

	public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

	public static Negociacoes fromJsonToNegociacoes(String json) {
        return new JSONDeserializer<Negociacoes>().use(null, Negociacoes.class).deserialize(json);
    }

	public static String toJsonArray(Collection<Negociacoes> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

	public static Collection<Negociacoes> fromJsonArrayToNegociacoeses(String json) {
        return new JSONDeserializer<List<Negociacoes>>().use(null, ArrayList.class).use("values", Negociacoes.class).deserialize(json);
    }

	public List<LancamentosAlunos> getNegociacaoLancamentosAlunos() {
        return this.negociacaoLancamentosAlunos;
    }

	public void setNegociacaoLancamentosAlunos(List<LancamentosAlunos> negociacaoLancamentosAlunos) {
        this.negociacaoLancamentosAlunos = negociacaoLancamentosAlunos;
    }

	public List<MovimentacoesCaixas> getNegociacaoMovimentacaoCaixas() {
        return this.negociacaoMovimentacaoCaixas;
    }

	public void setNegociacaoMovimentacaoCaixas(List<MovimentacoesCaixas> negociacaoMovimentacaoCaixas) {
        this.negociacaoMovimentacaoCaixas = negociacaoMovimentacaoCaixas;
    }

	public Double getDescontoReais() {
        return this.descontoReais;
    }

	public void setDescontoReais(Double descontoReais) {
        this.descontoReais = descontoReais;
    }

	public Usuario getAutorizador() {
        return this.autorizador;
    }

	public void setAutorizador(Usuario autorizador) {
        this.autorizador = autorizador;
    }

	public Double getTotalNegociacao() {
        return this.totalNegociacao;
    }

	public void setTotalNegociacao(Double totalNegociacao) {
        this.totalNegociacao = totalNegociacao;
    }

	public String getJustificativaNegociacao() {
        return this.justificativaNegociacao;
    }

	public void setJustificativaNegociacao(String justificativaNegociacao) {
        this.justificativaNegociacao = justificativaNegociacao;
    }

	public Controle getControle() {
        return this.controle;
    }

	public void setControle(Controle controle) {
        this.controle = controle;
    }
}
